﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Web;
using System.Web.Mvc;

namespace PMS.Controllers.BD
{
    public class DataDicController : Controller
    {
        public ActionResult DataDicList()
        {
            return View();
        }
        //查询
        public ActionResult List(BD_DataDic DataDic)
        {
            List<SearchCondition> listSearch = new List<SearchCondition>();
            if (Request.Form["searchData"] != null)
            {
                string searchData = Request.Form["searchData"];
                listSearch = ModelTools.GetSearchContions(searchData);
            }
            listSearch.Add(new SearchCondition() { Field = "DelState", Operator = "=", Value = "0" });
            Expression<Func<BD_DataDic, bool>> exp = SearchExpression.FindByGroup<BD_DataDic>(DataDic, listSearch);
            string OrderId = OperateContext.Sort == null ? "Id" : OperateContext.Sort;
            int count = 0;
            var list = BLLSession.Current.BD_DataDicBLL.GetPagedList(OperateContext.Page, OperateContext.Rows, exp, t => t.ID, true);
            count = BLLSession.Current.BD_DataDicBLL.GetCount(exp);
            return Json(new EasyuiDataPager() { rows = list, total = count });
        }
        //添加
        public ActionResult AddDataDic(BD_DataDic DD)
        {
            var list1 = DALSession.Current.BD_DataDicDAL.GetListBy(d => d.DataDicItem == DD.DataDicItem && d.DataDicType == DD.DataDicType && d.DelState == "0");
            if (list1.Count == 0)
            {
                DD.EditTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                DD.EditPerName = OperateContext.sessUserInfo.Name;
                DD.EditPerID = OperateContext.sessUserInfo.Id;
                DD.DelState = "0";
                BLLSession.Current.BD_DataDicBLL.Add(DD);
                return Json(new Message() { msg = "保存成功", state = "success" });
            }
            else
            {
                return Json(new Message() { msg = "数据已存在！", state = "warning" });
            }
        }
        //修改
        public ActionResult EditDataDic(BD_DataDic DD)
        {
            DD.EditTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
            DD.EditPerName = OperateContext.sessUserInfo.Name;
            DD.EditPerID = OperateContext.sessUserInfo.Id;
            BLLSession.Current.BD_DataDicBLL.ModifyBy(DD, t => t.ID == DD.ID, "DataDicType", "DataDicItem", "EditPerID", "EditPerName", "EditTime", "Note", "DelState");
            return Json(new Message() { msg = "保存成功", state = "success" });
        }
        //删除
        public ActionResult DelDataDic(int Id)
        {
            //BLLSession.Current.BD_DataDicBLL.DelBy(d => d.ID == Id);//直接删除
            BD_DataDic BD_DataDic1 = DALSession.Current.BD_DataDicDAL.GetListBy(d => d.ID == Id).FirstOrDefault();
            BD_DataDic1.EditTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
            BD_DataDic1.EditPerName = OperateContext.sessUserInfo.Name;
            BD_DataDic1.EditPerID = OperateContext.sessUserInfo.Id;
            BD_DataDic1.DelState = "1";
            BLLSession.Current.BD_DataDicBLL.ModifyBy(BD_DataDic1, t => t.ID == BD_DataDic1.ID, "DataDicType", "DataDicItem", "EditPerID", "EditPerName", "EditTime", "Note", "DelState");
            return Json(new Message() { msg = "删除成功", state = "success" });
        }
        //获取其他数据字典的某一类所有项
        public void GetDataDicItems()
        {
            string DataDicType1 = Request.QueryString["DataDicType"];
            Response.Write(JsonHelper.toJson(DALSession.Current.BD_DataDicDAL.GetListBy(t => t.DelState == "0" && t.DataDicType == DataDicType1).Select(t => new { value = t.ID, text = t.DataDicItem })));
        }
        //获取特定ID的数据字典项目名称
        public void GetDataDicItemName()
        {
            string DataDicItemID = Request.Form["DataDicItemID"];
            int DataDicItemIDint = Convert.ToInt32(DataDicItemID);
            var con = DALSession.Current.BD_DataDicDAL.GetListBy(c => c.ID == DataDicItemIDint);
            if (con.Count > 0)
            {
                Response.Write(JsonHelper.toJson(con.FirstOrDefault()));
            }
            else
            {
                Response.Write(JsonHelper.toJson(new { ID = DataDicItemID, TermType = DataDicItemID }));
            }
        }
    }
}
